package org.balabala.gateway.redis;

import lombok.extern.slf4j.Slf4j;
import org.balabala.gateway.entity.Comment;
import org.balabala.gateway.entity.Video;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.*;

@Service
@Slf4j
public class VideoManager {
    @Autowired
    private RedisTemplate redisTemplate;

    //缓存最近播放的视频信息
    private static final String VIDEO_KEY = "videos";

    //缓存视频评论信息
    private static final String COMMENT_KEY = "comment";

    //缓存热度视频
    private static final String HOT_VIDEOS_KEY = "hotVideos";

    //热度视频有效时间
    private static final Integer HOT_VIDEOS_EXPIRE_TIME = 3;

    private Jackson2JsonRedisSerializer<Video> videoJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Video.class);


    /**
     * 获取redis中所有的视频信息
     * @return
     */
    @SuppressWarnings("unchecked")
    public Map<Long, Video> getAllVideos(){
        redisTemplate.setHashValueSerializer(videoJackson2JsonRedisSerializer);
        Map<Long, Video> entries = redisTemplate.opsForHash().entries(VIDEO_KEY);
        return entries;
    }


    /**
     * 获取视频信息
     * @param key
     * @return
     */
    @SuppressWarnings("unchecked")
    public Video getVideo(Long key){
        redisTemplate.setHashValueSerializer(videoJackson2JsonRedisSerializer);
        return (Video) redisTemplate.opsForHash().get(VIDEO_KEY,key);
    }

    /**
     * 添加视频信息
     * @param video
     */
    @SuppressWarnings("unchecked")
    public void setVideo(Video video){
        if (video == null){
            log.error("vide is null !");
            return;
        }
        redisTemplate.setHashValueSerializer(videoJackson2JsonRedisSerializer);
        redisTemplate.opsForHash().put(VIDEO_KEY,video.getVideoId(),video);
    }


    /**
     * 删除缓存中的视频信息
     * @param videoId
     */
    @SuppressWarnings("unchecked")
    public void deleteVideo(Long videoId){
        if (videoId == null){
            return;
        }
        redisTemplate.opsForHash().delete(VIDEO_KEY,videoId);
    }


    /**
     * 添加热度视频
     * @param videos
     */
    @SuppressWarnings("unchecked")
    public void setHotVideos(Video[] videos){
        for (Video video : videos) {
            redisTemplate.opsForList().rightPush(HOT_VIDEOS_KEY,video);
        }
        redisTemplate.expire(HOT_VIDEOS_KEY,3, TimeUnit.MINUTES);
        log.info("hot videos set success,expire time is " + HOT_VIDEOS_EXPIRE_TIME + "MINUTES");
    }


    /**
     * 获取视频播放量
     * @return
     */
    @SuppressWarnings("unchecked")
    public List getHotVideos(Integer limit){
        return redisTemplate.opsForList().range(HOT_VIDEOS_KEY,0,limit);
    }


    /**
     * 添加热度视频的评论
     * @param videoId
     * @param comment
     */
    @SuppressWarnings("unchecked")
    public void setVideoComment(int videoId, Comment comment){
        String key = COMMENT_KEY+videoId;
        redisTemplate.opsForList().leftPush(key,comment);
        redisTemplate.expire(key, 60, TimeUnit.MINUTES);
    }

    /**
     * 获取热度视频评论
     * @param videoId
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Comment> getVideoComment(int videoId){
        String key = COMMENT_KEY+videoId;
        return redisTemplate.opsForList().range(key,0,-1);
    }

    /**
     * 删除视频评论
     * @param videoId
     * @param comment
     */
    @SuppressWarnings("unchecked")
    public void removeVideoComment(int videoId,Comment comment){
        String key = COMMENT_KEY+videoId;
        redisTemplate.opsForList().remove(key,1,comment);
    }


    /**
     * 获取视频评论数量
     * @param videoId
     * @return
     */
    @SuppressWarnings("unchecked")
    public Long getVideoCommentSize(int videoId){
        String key = COMMENT_KEY+videoId;
        return redisTemplate.opsForList().size(key);
    }

}
